package com.uxhao.aclservice.service.impl;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.uxhao.aclservice.entity.User;
import com.uxhao.aclservice.entity.UserRole;
import com.uxhao.aclservice.mapper.UserMapper;
import com.uxhao.aclservice.service.UserRoleService;
import com.uxhao.aclservice.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserRoleService userRoleService;

    @Override
    public User selectByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public Boolean saveRoleUserRealtionShip(String userId, List<String> roleIds) {
        Boolean flag = true;
        //1.删除原来的所有权限
        userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id", userId));
        //2.重新设置权限
        for (String roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            userRole.setIsDeleted(false);
            if(!userRoleService.save(userRole)){
                flag=false;
            }
        }
        return flag;
    }
}
